<?php

/**
 * This is the model class for table "post".
 *
 * The followings are the available columns in table 'post':
 * @property integer $id
 * @property string $judul
 * @property string $image_header
 * @property string $image_excerpt
 * @property string $konten
 * @property string $konten_excerpt
 * @property integer $create_by
 * @property string $create_time
 * @property integer $update_by
 * @property string $update_time
 * @property integer $halal
 * @property integer $daging
 * @property integer $sayuran
 * @property integer $buah
 * @property integer $susu
 * @property integer $telur
 * @property integer $tumbuhan_polong
 * @property integer $berbiji
 * @property integer $padi_padian
 * @property integer $jumlah_kalori
 * @property integer $chef
 * @property integer $active
 * @property string $price
 *
 * The followings are the available model relations:
 * @property PostCategory[] $postCategories
 * @property PostIngredient[] $postIngredients
 * @property PostView[] $postViews
 * @property Star[] $stars
 */
class Post extends ActiveRecord
{
	public $categories;
	
	public function saveCategories(){
		$this->deleteCurrentCategories();
		
		foreach($this->categories as $index=>$value){
			$kategori = Category::model()->findByAttributes(array('nama'=>$value));
			if(empty($kategori)){
				$newKategori = new Category();
				$newKategori->nama = $value;
				$newKategori->save(false);
				$kategori = $newKategori;
			}
			
			$postCategory = new PostCategory;
			$postCategory->category_id = $kategori->id;
			$postCategory->post_id = $this->id;
			if(!$postCategory->save()){
				$this->addError('id',$postCategory->getErrors());
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Delete seluruh kategori pada table post_category untuk item ini
	 */
	public function deleteCurrentCategories(){
		Yii::app()->db->createCommand("UPDATE post_category SET active = '0' WHERE post_id = $this->id;")->execute();
	}
	
	public function saveIncludingCategories($runValidation=true,$attributes=null)
	{
		return $this->save() && $this->saveCategories();
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'post';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('judul, konten, konten_excerpt','required','on'=>'insert'),
			array('create_by, update_by, halal, daging, sayuran, buah, susu, telur, tumbuhan_polong, berbiji, padi_padian, jumlah_kalori, chef, active', 'numerical', 'integerOnly'=>true),
			array('judul', 'length', 'max'=>45),
			array('image_header, image_excerpt', 'length', 'max'=>255),
			array('konten_excerpt', 'length', 'max'=>200),
			array('price', 'length', 'max'=>14),
			array('konten, create_time, update_time, categories', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, judul, image_header, image_excerpt, konten, konten_excerpt, create_by, create_time, update_by, update_time, halal, daging, sayuran, buah, susu, telur, tumbuhan_polong, berbiji, padi_padian, jumlah_kalori, chef, active, price', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'postCategories' => array(self::HAS_MANY, 'PostCategory', 'post_id'),
			'postIngredients' => array(self::HAS_MANY, 'PostIngredient', 'post_id'),
			'postViewers' => array(self::HAS_MANY, 'PostView', 'post_id'),
			'stars' => array(self::HAS_MANY, 'Star', 'post_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'judul' => 'Judul',
			'image_header' => 'Image Header',
			'image_excerpt' => 'Image Excerpt',
			'konten' => 'Konten',
			'konten_excerpt' => 'Konten Excerpt',
			'create_by' => 'Create By',
			'create_time' => 'Create Time',
			'update_by' => 'Update By',
			'update_time' => 'Update Time',
			'halal' => 'Halal',
			'daging' => 'Daging',
			'sayuran' => 'Sayuran',
			'buah' => 'Buah',
			'susu' => 'Susu',
			'telur' => 'Telur',
			'tumbuhan_polong' => 'Tumbuhan Polong',
			'berbiji' => 'Berbiji',
			'padi_padian' => 'Padi Padian',
			'jumlah_kalori' => 'Jumlah Kalori',
			'chef' => 'Chef',
			'active' => 'Active',
			'price' => 'Price',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('judul',$this->judul,true);
		$criteria->compare('image_header',$this->image_header,true);
		$criteria->compare('image_excerpt',$this->image_excerpt,true);
		$criteria->compare('konten',$this->konten,true);
		$criteria->compare('konten_excerpt',$this->konten_excerpt,true);
		$criteria->compare('create_by',$this->create_by);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_by',$this->update_by);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('halal',$this->halal);
		$criteria->compare('daging',$this->daging);
		$criteria->compare('sayuran',$this->sayuran);
		$criteria->compare('buah',$this->buah);
		$criteria->compare('susu',$this->susu);
		$criteria->compare('telur',$this->telur);
		$criteria->compare('tumbuhan_polong',$this->tumbuhan_polong);
		$criteria->compare('berbiji',$this->berbiji);
		$criteria->compare('padi_padian',$this->padi_padian);
		$criteria->compare('jumlah_kalori',$this->jumlah_kalori);
		$criteria->compare('chef',$this->chef);
		$criteria->compare('active',$this->active);
		$criteria->compare('price',$this->price,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Post the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
